// 冒泡 稳定
function bubbleSort(arr) {
    for (let i = 0; i < arr.length; i++) {
        for (let j = 0; j < arr.length - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                const tmp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = tmp;
            }
        }
    }
    return arr;
}

// 插排 稳定
function insertSort(arr) {
    for (let i = 1; i < arr.length; i++) {
        let j = i - 1;
        const tmp = arr[i];
        while (j >= 0 && tmp < arr[j]) {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = tmp;
    }
    return arr;
}

// 选择排序 不稳定
function selectSort(arr) {
    for (let i = 0, k = 0; i < arr.length; i++, k = i) {
        for (let j = i + 1; j < arr.length; j++) {
            if (arr[k] > arr[j]) {
                k = j;
            }
        }
        if (i != k) {
            const tmp = arr[i];
            arr[i] = arr[k];
            arr[k] = tmp;
        }
    }
    return arr;
}

// 快排 不稳定
function quickSort(arr, left, right) {
    if (left >= right) return;
    let reference = arr[left];
    let i = left,
        j = right;
    while (i < j) {
        while (i < j && arr[j] >= reference) {
            j--;
        }
        while (i < j && arr[i] <= reference) {
            i++;
        }
        if (i < j) {
            const tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
        }
    }
    arr[left] = arr[i];
    arr[i] = reference;
    quickSort(arr, left, i - 1);
    quickSort(arr, i + 1, right);
}

// 归并排序 稳定 递归法
function mergeSort(arr, start = 0, end = arr.length - 1) {
    if (start >= end) return;
    let mid = start + ((end - start) >> 1);
    mergeSort(arr, start, mid);
    mergeSort(arr, mid + 1, end);
    merge(arr, start, mid, end);
}

function merge(arr, start, mid, end) {
    let tmpArr = [];
    let p1 = start;
    let p2 = mid + 1;
    let index = 0;
    while (p1 <= mid && p2 <= end) {
        if (arr[p1] <= arr[p2]) {
            tmpArr[index++] = arr[p1++];
        } else {
            tmpArr[index++] = arr[p2++];
        }
    }
    while (p1 <= mid) {
        tmpArr[index++] = arr[p1++];
    }
    while (p2 <= end) {
        tmpArr[index++] = arr[p2++];
    }

    for (let i = 0; i < tmpArr.length; i++) {
        arr[i + start] = tmpArr[i];
    }
}

// 归并排序 迭代法 稳定
function mergeSort2(arr) {
    const len = arr.length;
    let res = [];
    let gap, start;
    for (gap = 1; gap < len * 2; gap *= 2) {
        for (start = 0; start < len; start += gap * 2) {
            let low = start;
            let mid = start + gap < len ? start + gap : len;
            let high = start + gap * 2 < len ? start + gap * 2 : len;

            let start1 = low,
                end1 = mid;
            let start2 = mid,
                end2 = high;

            while (start1 < end1 && start2 < end2) {
                res[low++] =
                    arr[start1] < arr[start2] ? arr[start1++] : arr[start2++];
            }
            while (start1 < end1) {
                res[low++] = arr[start1++];
            }
            while (start2 < end2) {
                res[low++] = arr[start2++];
            }
        }
        const tmp = res;
        res = arr;
        arr = tmp;
    }
    res = arr;
    return res;
}

// 堆排序 不稳定
function setMaxHeapify(heap, start, end) {
    son = start * 2;
    while (son <= end) {
        if (son + 1 <= end && heap[son + 1] > heap[son]) {
            son += 1;
        }
        if (heap[son] > heap[start]) {
            swap(heap, son, start);
            start = son;
            son = son * 2;
        } else {
            break;
        }
    }
}

function heapSort(arr) {
    const root = 1;
    const heap = [null, ...arr];
    const len = heap.length;

    // 构造大根堆
    for (let i = (len / 2) | 0; i >= root; i--) {
        setMaxHeapify(heap, i, len - 1);
    }

    for (let i = len - 1; i >= root; i--) {
        swap(heap, i, root);
        setMaxHeapify(heap, root, i - 1);
    }

    return heap.slice(1);
}

function swap(arr, i, j) {
    const tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}

const demo = [9, 8, 1, 3, 5, 2, 7, 4, 6];
// console.log(bubbleSort(demo))
// console.log('插入排序：', insertSort(demo))
// console.log(selectSort(demo))

// quickSort(demo, 0, demo.length - 1)
// console.log(demo)

// mergeSort(demo)
// console.log(demo)
// console.log(mergeSort2(demo))

console.log(heapSort(demo));
